#
# Copyright 2008 Spencer Davis <SpencerDavis91@gmail.com>
#
# This file is part of Tuxedo.
#
# Tuxedo is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# Tuxedo is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with Tuxedo; if not, see <http://gnu.org/licenses/.>
#

import os
import sys
import gtk
import gtk.glade
import task
from task import Task
from database import Database
from config import Config

class EditDialog:
    """Creates the dialog box for editing a task."""
    def onApply(self, data):
        """Reads the information from the dialog and saves the task."""
        # Get the information from the dialog and save it to the database
        t = Task()

        # Make sure that there was text in the text box
        if len(self.widgets.get_widget('TaskEntry').get_text()) > 1:
            # Set the task name to the value of the text box
            t.name = self.widgets.get_widget('TaskEntry').get_text()

            # Set the priority to the value of the combo box
            t.priority = self.widgets.get_widget('PriorityEntry').get_active()

            # Set the status to not completed
            t.status = self.widgets.get_widget('StatusEntry').get_active()

            # Set the id of the task
            t.id = self.taskid

            # Set the due date of the task
            t.duedate = self.widgets.get_widget('DateEntry').get_date()

            # Save the task in the database
            self.db.saveTask(t)

        # Close the dialog
        self.dialog.destroy()

    def onCancel(self, data):
        """Closes the dialog."""
        self.dialog.destroy()

    def __init__(self, db, t):
        """Reads the glade file and creates the dialog with the task's current data already entered."""
        # Set the path to the glade file to be the path to Tuxedo.py
        gladefile = os.path.join(os.path.dirname(sys.argv[0]), 'EditDialog.glade')

        # Read the widgets from the glade file
        self.widgets = gtk.glade.XML(gladefile)

        # Store the signals in a dictionary
        signals = {"on_Apply" : self.onApply,
                   "on_Cancel" : self.onCancel}

        # Autoconnect the signals
        self.widgets.signal_autoconnect(signals)

        # Store the database connection that was passed
        self.db = db

        # Store the dialog widget
        self.dialog = self.widgets.get_widget('EditDialog')

        # Fill priorities combobox with names of priorities
        config = Config()
        for priority in config.priorityNames():
            self.widgets.get_widget('PriorityEntry').append_text(priority)

        # Fill statuses combobox with names of statuses
        for status in config.statusNames():
            self.widgets.get_widget('StatusEntry').append_text(status)

        # Set the values to be the task's current properties
        self.widgets.get_widget('TaskEntry').set_text(t.name)
        self.widgets.get_widget('PriorityEntry').set_active(t.priority)
        self.widgets.get_widget('StatusEntry').set_active(t.status)
        self.widgets.get_widget('DateEntry').select_month(t.duedate[1], t.duedate[0])
        self.widgets.get_widget('DateEntry').select_day(t.duedate[2])

        # Store the id of the task that is being edited
        self.taskid = t.id
